import axios from "axios"
import { prefix } from './api'

/**
 * 用户登录
 * @param {登录用户} customer 
 */
export async function login(customer) {
    let state;
    // console.log('api')
    await axios.post(
        prefix + '/login',
        { customer }
    ).then(res => {

        state = (res.code === undefined || res.code !== 200) ? null : res.data
    }).catch(err => { console.log(err) })
    return state
}

/**
 * 记住登录用户信息是否发生改变
 * 200 记住登录 未修改用户信息 
 * 210 记住登录 已修改用户密码或用户名 需要重新登录
 * 220 记住登录 已修改用户其他信息  需要更新用户信息
 * 
 * @param {*} params 
 */
export async function isrememberLogin(customer, account) {
    // console.log('判断是否记住登录')
    // console.log(customer+'#'+account+'#'+token)

    await axios.post(
        prefix + '/isrememberLogin',
        { customer, account }
    ).then(res => {
        const code = res.code
        switch (code) {
            case 200:
                account.time = '2011-11-11 20:22:22';
                break;
            case 210:
                alert('用户关键信息已改变,请重新登录');
                customer = null; account = null;
                break;
            case 220:
                //哪个是关键信息 由后台判断
                alert('用户其他信息发生了改变')
                customer = res.data.customer;
                account = res.data.account;
                break
            default:
                window.localStorage.removeItem('customer')
                window.localStorage.removeItem('account')
                customer = null; account = null;
                break;
        }
    })
    return { customer, account }
}